package com.yq.day28._01lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 3307235355@qq.com
 * @since 2022/06/28 17:44
 */
public class Demo01 {
    public static void main(String[] args) {
        Sell sell = new Sell();
        new Thread(sell,"a").start();
        new Thread(sell,"b").start();
        new Thread(sell,"c").start();

    }
}
class Sell implements Runnable {
    int tickets = 100;
    Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true) {

            lock.lock();
            try {
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "卖了" + (tickets--) + "票");
                }

            }finally {
                lock.unlock();
            }



        }
    }
}